package com.example.wechattakeaway.utils;


import com.example.wechattakeaway.enums.ResultEnum;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

public class FileUpload {

    public static String UploadPicture (MultipartFile file){
        if (file.isEmpty())
            return ResultVOUtil.error(ResultEnum.FILE_NOT_EXIST).toString();
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "D://image//"; // 上传后的路径,即本地磁盘
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "http://10608c306b194e41.natapp.cc:10015/image/" + fileName;//本地目录和生成的文件名拼接，这一段存入数据库
        HashMap imgMap = new HashMap();
        imgMap.put("imgUrl", filename);

        return filename;

    }

}
